<?php

namespace App\Http\Controllers\Administrator\Game;

use Carbon\Carbon;
use App\Models\GameLine;
use App\Models\GameList;
use App\Models\GameType;
use App\Models\GameCategory;
use Illuminate\Http\Request;
use App\Models\Def\MainGamePlat;
use App\Http\Controllers\Controller;
use App\Http\Requests\Administrator\GameListRequest;
use Ramsey\Uuid\Uuid;

class GameListController extends Controller
{
    /**
     * gameapi
     * 2018/10/21 22:59
     * Administrator
     * index
     * Display a listing of the resource.
     * @param Request $request
     * @param GameList $gameList
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function index(Request $request, GameList $gameList)
    {

        $this->authorize('index', $gameList);

        $where = [];
        $params = [
            'game_code' => '',
            'main_game_plat_code' => '',
            'status' => '',
            'cn_name' => ''
        ];

        if (!empty($request->main_game_plat_code)) {
            $where[] = ['main_game_plat_code', '=', $request->main_game_plat_code];
            $params['main_game_plat_code'] = $request->main_game_plat_code;
        }

        if (isset($request->status)) {
            $where[] = ['status', '=', (int)($request->status)];
            $params['status'] = (int)($request->status);
        }

        if (isset($request->cn_name)) {
            $where[] = ['cn_name', 'like', '%' . $request->cn_name . '%'];
            $params['cn_name'] = $request->cn_name;
        }


        $gameList = GameList::where($where)->with('gameCategory', 'gameType', 'gameLine');

        $limit = $request->limit ?? config('administrator.paginate.limit');
        $gameLists = $gameList->paginate((integer)$limit);
        $plats = MainGamePlat::all();

        return view('backend.game.game_list.index', compact('gameLists', 'params', 'limit', 'plats'));

    }

    /**
     * gameapi
     * 2018/10/21 23:00
     * Administrator
     * create
     * Show the form for creating a new resource.
     * @param GameList $gameList
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function create(GameList $gameList)
    {
        $this->authorize('create', $gameList);
        $gameCategories = GameCategory::all();
        $gameTypes = GameType::all();
        $gameLines = GameLine::all();
        $plats = MainGamePlat::all();
        return view('backend.game.game_list.create_and_edit', compact('gameList', 'gameTypes', 'gameCategories', 'gameLines', 'plats'));
    }

    /**
     * gameapi
     * 2018/10/21 23:00
     * Administrator
     * store
     * Store a newly created resource in storage.
     * @param GameListRequest $request
     * @param GameList $gameList
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function store(GameListRequest $request, GameList $gameList)
    {
        $this->authorize('create', $gameList);
        $params = array_filter($request->validated(), 'filterCallback');
        $unique_key = Uuid::uuid4();;
        $params = array_merge($params, ['unique_key' => $unique_key]);
        GameList::create($params);

        return redirect()->route('game_list.index')->with('success', '添加成功.');
    }

    /**
     * gameapi
     * 2018/10/21 22:13
     * Administrator
     * show
     * Display the specified resource.
     * @param $id
     */
    public function show($id)
    {
        //
    }

    /**
     * gameapi
     * 2018/10/21 23:01
     * Administrator
     * edit
     * Show the form for editing the specified resource.
     * @param GameList $gameList
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function edit(GameList $gameList)
    {
        $this->authorize('update', $gameList);
        $gameCategories = GameCategory::all();
        $gameTypes = GameType::all();
        $gameLines = GameLine::all();
        $plats = MainGamePlat::all();
        return view('backend.game.game_list.create_and_edit', compact('gameList', 'gameTypes', 'gameCategories', 'gameLines', 'plats'));
    }

    /**
     * gameapi
     * 2018/10/21 23:01
     * Administrator
     * update
     * Update the specified resource in storage.
     * @param GameListRequest $request
     * @param GameList $gameList
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function update(GameListRequest $request, GameList $gameList)
    {
        $this->authorize('update', $gameList);
        //$params = array_filter($request->validated(), 'filterCallback');
        $params = $request->validated();
        if (empty($gameList->unique_key) || $gameList->unique_key === '') {
            $unique_key = Uuid::uuid4();
            $params = array_merge($params, ['unique_key' => $unique_key]);
        }

        $gameList->update($params);
        return redirect()->route('game_list.index')->with('success', '修改成功.');
    }

    /**
     * gameapi
     * 2018/10/21 23:01
     * Administrator
     * destroy
     * Remove the specified resource from storage.
     * @param Request $request
     * @param GameList $gameList
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Auth\Access\AuthorizationException
     */
    public function destroy(Request $request, GameList $gameList)
    {
        $this->authorize('destroy', $gameList);
        $delIds = json_decode($request->getContent(), true);
        $delRes = GameList::whereIn('id', $delIds)->delete();
        if ($delRes) {
            return response()->json([
                'message' => '删除成功！受影响行数：' . $delRes,
            ], 200
            );

        } else {
            return response()->json([
                'message' => '删除失败！',
            ], 500
            );
        }
    }


}
